# 数据探索分析
#数据读取及基本处理
import numpy as np
import pandas as pd
# 绘图工具
import seaborn as sns
import matplotlib.pyplot as plt

# 1、读入数据
trian = pd.read_csv("E:/VC_project/data/day.csv")
# 显示前五行数据及基本信息，查看有无缺失信息
print(trian.head())


# 2、数据探索
# 2.1 离散数据的分布特征
# 对类别型特征，观察其取值范围及分布情况
categorical_features = ["season","mnth","weekday","weathersit"]
for i in categorical_features:
    print("{}属性的不同取值及出现的次数：".format(i))
    print(trian[i].value_counts())
    sns.countplot(trian[i])
    plt.show()
    #trian[i] = trian[i].astype("object") #将类别数据转换成object类型
print(trian.info())

# 2.2数值型的特征分布
#对数值型特征绘制直方图
number_features = ["temp","atemp","hum","windspeed"]
trian[number_features].hist()
plt.show()

# 3.特征与目标之间的关系
# 3.1每年的骑行量分布
# 使用提琴形图violinplot查看
sns.violinplot(data=trian[['yr','cnt']],x="yr",y="cnt")
plt.show()

# 3.2每天的骑行量分布
import datetime
trian["date"] = pd.to_datetime(trian["dteday"])
trian["dayofyear"] = trian["date"].dt.dayofyear

fig,ax = plt.subplots()
sns.pointplot(data=trian[["dayofyear","cnt","yr"]],x="dayofyear",y="cnt",hue="yr",ax=ax)
ax.set(title="dayly distribution of counts")
plt.show()

# 3.3季节与骑行数量的分布关系
sns.violinplot(data=trian[["season","cnt"]],x="season",y="cnt")
plt.show()

# 用barplot展示的是某种变量分布的平均值
sns.barplot(data=trian[["season","cnt"]],x="season",y="cnt")
plt.show()

# 3.4月份与骑行量的关系
sns.barplot(data=trian[["mnth","cnt"]],x="mnth",y="cnt")
plt.show()

# 3.5天气与骑行量的关系
sns.barplot(data=trian[["weathersit","cnt"]],x="weathersit",y="cnt")
plt.show()

# 3.6工作日和节假日的分布
fig,ax = plt.subplots(1,2)
sns.barplot(data=trian[["holiday","cnt"]],x="holiday",y="cnt",ax=ax[0])
sns.barplot(data=trian[["workingday","cnt"]],x="workingday",y="cnt",ax=ax[1])
plt.show()

# 3.7数值特征与骑行量之间的相关性
corrMatt = trian[["temp","atemp","hum","windspeed","casual","registered","cnt"]].corr()
mask = np.array(corrMatt)
mask[np.tril_indices_from(mask)] = False
sns.heatmap(corrMatt,mask=mask,vmax=.8,square=True,annot=True)
plt.show()

